package com.ytxg.health_management.healthModel.controller;

import com.ytxg.health_management.controller.UserController;
import com.ytxg.health_management.healthModel.entity.HealthModel;
import com.ytxg.health_management.healthModel.entity.HealthModelRecord;
import com.ytxg.health_management.healthModel.service.ModelService;
import com.ytxg.health_management.pojo.dto.UserDto;
import com.ytxg.health_management.pojo.entity.Result;
import com.ytxg.health_management.util.MyException;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletConfig;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/healthModel")
public class HealthModelController {

    @Resource
    private ModelService modelService;
    @Resource
    private UserController userController;
    private ServletConfig servletConfig;

    /**
     * 新增模型
     */
    @PostMapping("/add")
    public Result<String> addModel(@RequestBody HealthModel healthModel, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        Result<String> res = modelService.addModel(healthModel, userDtoResult.getData().getUserName());
        return res;
    }
    /**
     * 新增模型管理员
     */
    @PostMapping("/addAdmin")
    public Result<String> addAdmin(@RequestBody HealthModel healthModel, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        Result<String> res = modelService.addModelAdmin(healthModel);
        return res;
    }

    /**
     * 查询模型
     */
    @GetMapping("/getMyModel")
    public Result<List<HealthModel>> getMyModel(@RequestParam("modelType") String type, @RequestHeader("Authorization") String token) {
        if (type.equals("1")) {
            Result<UserDto> userDtoResult = userController.tokenAuth(token);
            return modelService.selectModel(new HealthModel().setCreateBy(userDtoResult.getData().getUserName()).setModelType(type));
        } else {
            return modelService.selectModel(new HealthModel().setModelType(type));
        }
    }
    /**
     * 查询模型管理员
     */
    @PostMapping("/getMyModelAll")
    public Result<Map<String,Object>> getMyModel(@RequestBody HealthModel healthModel, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return modelService.selectModelAll(healthModel);
    }

    /**
     * 删除模型
     */
    @GetMapping("/deleteModel")
    public Result<String> deleteModel(@RequestParam("id") int id, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return modelService.deleteModel(id);
    }

    /**
     * 新增模型记录
     */
    @PostMapping("/addModelRecord")
    public Result<String> addModelRecord(@RequestBody HealthModelRecord healthModelRecord, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return modelService.addModelRecord(healthModelRecord, userDtoResult.getData().getUserName());
    }

    /**
     * 查询模型记录
     */
    @GetMapping("/getModelRecord")
    public Result<List<HealthModelRecord>> getModelRecord(@RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return modelService.getModelRecord(userDtoResult.getData().getUserName());
    }

    /**
     * 删除模型记录
     */
    @GetMapping("/deleteModelRecord")
    public Result<String> deleteModelRecord(@RequestParam("id") int id, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return modelService.deleteModelRecord(id);
    }

    /**
     * 获取模型名
     */
    @GetMapping("/getModelName")
    public Result<Map<Integer, String>> getModelName(@RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return modelService.getModelName(userDtoResult.getData().getUserName());
    }

    /**
     * 获取图表数据
     */
    @GetMapping("/getChartData")
    public Result<ArrayList<Object>> getModegetChartDatalName(@RequestParam("id") int id, @RequestHeader("Authorization") String token) {
        Result<UserDto> userDtoResult = userController.tokenAuth(token);
        return modelService.getChartData(id, userDtoResult.getData().getUserName());
    }



}
